package com.leoman.controller;

import com.leoman.common.factory.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.Area;
import com.leoman.entity.User;
import com.leoman.service.AreaService;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by wangbin on 2015/7/29.
 */
@Controller
@RequestMapping(value = "admin/area")
public class AreaController extends CommonController {

    @Autowired
    private AreaService areaService;

    @RequestMapping(value = "/index")
    public String index() {
        return "反馈区域列表";
    }

    @RequestMapping(value = "/indexInfo")
    public String indexInfo(ModelMap model, Integer parentId, Integer level, Integer type) {
        Area area;

        // type为0时，为跳转到下级区域列表
        // type为1时，为返回到上级区域列表
        if (null == type || type == 0) {
            area = areaService.getById(parentId);
            model.addAttribute("parentId", parentId);
            model.addAttribute("level", level + 1);
        } else {
            area = areaService.getById(parentId);
            model.addAttribute("parentId", area.getParentId());
            model.addAttribute("level", level - 1);
        }

        model.addAttribute("area", area);

        return "反馈区域下级列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer parentId,
                     Integer level,
                     String name,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);

        Page<Area> page = areaService.page(parentId, level, name, pageNum, length);

        for (Area area : page.getContent()) {
            if (area.getLevel() > 1) {
                area.setParentName(areaService.getById(area.getParentId()).getName());
            }
        }

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(Integer id, String name, Integer type, Integer size, Integer parentId, Integer level) {
        try {
            Area area;

            if (null == id) {
                area = new Area();
            } else {
                area = areaService.getById(id);
            }

            area.setName(name);
            area.setSize(size);
            area.setParentId(parentId);
            area.setLevel(level);
            area.setIsCut(0);

            areaService.update(area);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer id) {
        try {
            areaService.deleteById(id);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/areaInfo")
    @ResponseBody
    public Area areaInfo(Integer areaId) {
        return areaService.getById(areaId);
    }
}
